package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouShanShuiMgr;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Random;

/**
 * 玉门关
 */
public class YuMenGuanGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;
        if(!stop){
            return;
        }

        //测试，关掉玉门关
        if(Config.isDebug()){
            //空白格子
//            ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, false);
//            GridHandleResult gridHandleResult = new GridHandleResult();
//            gridHandleResult.setGridParam(gridParam);
//
//            XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
//            builder.setPosition(pos);
//            builder.setGridId(gridId);
//            builder.setHandle(true);
//            gridHandleResult.setBuilder(builder);
//
//            result.getGridHandleResultList().add(gridHandleResult);
//            return;
        }

        if(result.getUserData().isDrift()){

            Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, true);

            GridHandleResult gridHandleResult = new GridHandleResult();
            XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
            builder.setPosition(pos);
            builder.setGridId(gridId);
            builder.setHandle(true);
            builder.setIsDrift(true);
            builder.setAward(PropertyHelper.parsePropertyToString(fixedReward));
            builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));

            gridHandleResult.setBuilder(builder);

            int index = result.getActivity().getConfig().getSHANSHUI_MAP().indexOf(16);
            ShanShuiGridParam gridParam = new ShanShuiGridParam(16, index, true, true);
            gridHandleResult.setGridParam(gridParam);
            result.getGridHandleResultList().add(gridHandleResult);


            //如果是再次回到玉门关，没有这个功能
//            int driftTeamId = result.getUserData().getMapTeamId();
//            Property property = result.getActivity().reBackFromDrift(result.getUserData(), false, -4);
//            GridHandleResult gridHandleResult = new GridHandleResult();
//
//            XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
//            builder.setAward(PropertyHelper.parsePropertyToString(property));
//            builder.setPosition(pos);
//            builder.setGridId(gridId);
//            builder.setHandle(true);
//            builder.setEventId(-4);
//            builder.setIsDrift(true);
//            builder.setAward(PropertyHelper.parsePropertyToString(property));
//
//            gridHandleResult.setBuilder(builder);
//
//            int index = result.getActivity().getConfig().getSHANSHUI_MAP().indexOf(16);
//            ShanShuiGridParam gridParam = new ShanShuiGridParam(16, index, true, true);
//            gridParam.setEventId(-4);
//            gridHandleResult.setGridParam(gridParam);
//            result.getGridHandleResultList().add(gridHandleResult);
//
//            //玉门关归来-队伍日志
//            if(driftTeamId > 0) {
//                CommonTeam team = CommonTeamMgr.getTeam(result.getConfig().getActivityInfo().getActivityId(), 0, driftTeamId);
//                result.getActivity().createActionLogData(result.getUserData(), XingYouShanShuiProto.ShanShuiLogTypeEnum.SHAN_SHUI_LOG_FERRY_TERMINAL, builder.getAward(),
//                        Arrays.asList(String.valueOf(result.getUserData().getRoleId()), team == null ? "" : team.getTeamName()));
//            }

        }else {

            Property property = new Property();

            Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, true);

            property.addProperty(fixedReward);

            GridHandleResult gridHandleResult = new GridHandleResult();
            //漂泊骰子
            property.addProperty(result.getConfig().getSHANSHUI_WANDER_DICE_ID(), gridInfo.getParam().get(0));

            CrossXingYouShanShuiTeamData myTeamData = result.getActivity().getTeamData(result.getUserData().getTeamId());
            int targetTeamId = 0;
            synchronized (result.getActivity().getTeamLocker(myTeamData.getTeamId())){
                if(myTeamData.getRandomTodayTeam().size() < 5){
                    myTeamData.getRandomTodayTeam().addAll(result.getActivity().randomTeam(myTeamData));
                    myTeamData.setUpdateOption();
                }
                while (myTeamData.getRandomTodayTeam().size() > 0) {
                    targetTeamId = myTeamData.getRandomTodayTeam().remove(0);
                    CrossXingYouShanShuiTeamData teamData = result.getActivity().getTeamData(targetTeamId);
                    if(teamData.getVisitorMap().size() < result.getConfig().getSHANSHUI_MAP_NUM_LIMIT()){
                        myTeamData.addTodayTeam(targetTeamId);
                        myTeamData.setUpdateOption();
                        result.getUserData().setMapTeamId(targetTeamId);
                        break;
                    }
                }
                if(targetTeamId == 0){
                    //实现没有了，就随机一个今日已访问过的
                    if(myTeamData.getTodayTeam().size() > 0){
                        int i = new Random().nextInt(myTeamData.getTodayTeam().size());
                        targetTeamId = myTeamData.getTodayTeam().get(i);
                        result.getUserData().setMapTeamId(targetTeamId);
                    }
                }
            }

            if(targetTeamId == 0){
                List<Integer> collection = result.getActivity().randomTeam(myTeamData);
                if(collection.isEmpty()){
                    result.setRet(GameErrorCode.E_XING_YOU_SHAN_SHUI_TEAM_NUM_NO_ENOUGH);
                    CrossXingYouShanShuiMgr.getLogger().error("活动{}， 玩家{}， 无其他队伍可以漂泊", result.getUserData().getActivityId(), result.getUserData().getUserId());
                    return;
                }
                targetTeamId = collection.get(0);
                result.getUserData().setMapTeamId(targetTeamId);
            }

            CrossXingYouShanShuiTeamData driftTeamData = result.getActivity().getTeamData(targetTeamId);
            driftTeamData.getVisitorMap().put(result.getUserData().getUserId(), System.currentTimeMillis());
            driftTeamData.setUpdateOption();

            XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
            builder.setAward(PropertyHelper.parsePropertyToString(property));
            builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));
            builder.setPosition(pos);
            builder.setGridId(gridId);
            builder.setIsDrift(false);
            gridHandleResult.setBuilder(builder);

            ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, true, false);
            gridHandleResult.setGridParam(gridParam);

            result.getGridHandleResultList().add(gridHandleResult);
        }
    }
}
